package com.geen.tuoke.ui.viewmodel

import androidx.lifecycle.MutableLiveData
import com.geen.tuoke.base.BaseViewModel
import com.geen.tuoke.bean.CustomerInfo
import com.geen.tuoke.bean.DataPage
import com.geen.tuoke.bean.TaskDetailInfo
import com.geen.tuoke.bean.TaskInfo
import com.geen.tuoke.bean.request.AddTaskRequest
import com.geen.tuoke.bean.request.RecreateTaskRequest
import com.geen.tuoke.bean.request.UpdateTaskRequest
import com.geen.tuoke.bean.request.UpdateTaskMobile
import com.geen.tuoke.network.api.apiService
import com.geen.tuoke.utils.ext.request
import com.geen.tuoke.utils.ext.requestNoCheck
import com.geen.tuoke.utils.ext.toastShort

/**
 *
 * @Description:
 * @Author: geen
 * @CreateDate: 2022/1/3 16:06
 * @Version: 1.0
 */
class PhoneFragmentViewModel : BaseViewModel() {

    var pageNo = 1
    var pageSize = 20
    var datapage = MutableLiveData<DataPage<TaskInfo>>()
    var detailsList = MutableLiveData<List<TaskDetailInfo>>()
    var timeInterval = MutableLiveData<Int>()
    var addTaskResult = MutableLiveData<Boolean>()
    var reCreateTaskResult = MutableLiveData<Boolean>()

    //0 未接通 1未接通    -1 空号
    var callOutResult = MutableLiveData<Int>()
    var deleteTaskResult = MutableLiveData<Int>()
    val customerInfo = MutableLiveData<CustomerInfo>()

    var updateMobile = MutableLiveData<UpdateTaskMobile>()

    init {

    }

    fun getPhoneTask(showLoading: Boolean = true) {
        request({ apiService.getPhoneTask(pageNo, pageSize) }, {
            datapage.value = it
        }, {
            toastShort(it.errorMsg)
        }, showLoading)
    }

    fun getPhoneTaskDetailById(
        id: Int,
        isDurationSort: Boolean = false,
        isCallTimeSort: Boolean = false
    ) {
        request({ apiService.getPhoneTaskDetailById(taskId = id) }, {
            var result=it.records
            if(isDurationSort){
                result=result.sortedByDescending {
                    it.duration
                }
            }
            if(isCallTimeSort){
                result=result.sortedByDescending {
                    it.createTime
                }
            }
            detailsList.value =result
        }, {
            toastShort(it.errorMsg)
        }, true)
    }

    fun updatePhoneTaskInterval(id: Int, time: Int) {
        request({ apiService.updatePhoneTaskInterval(taskId = id, interval = time) }, {
            timeInterval.value = time
        }, {
            toastShort(it.errorMsg)
        }, true)
    }


    fun addPhoneTask(task: AddTaskRequest) {
        request({ apiService.addPhoneTask(task) }, {
            addTaskResult.value = true
        }, {
            toastShort(it.errorMsg)
        }, true)
    }

    //重建任务
    fun recreatePhoneTask(task: RecreateTaskRequest) {
        request({ apiService.recreatePhoneTask(task) }, {
            reCreateTaskResult.value = true
        }, {
            toastShort(it.errorMsg)
        }, true)
    }

    fun deletePhoneTask(itemId: Int, taskId: Int) {
        request({ apiService.deletePhoneTask(itemId, taskId) }, {
            deleteTaskResult.value = itemId
        }, {
            toastShort(it.errorMsg)
        }, true)
    }


    fun callout(itemId: String, taskId: String, mobile: String) {
        requestNoCheck({ apiService.callout(itemId, taskId, mobile) }, {

            it.data.let {
                //0 未接通 1接通    -1 空号
                val result = if (it == "0000") {
                    1
                } else if (it == "1002" || it == "1003") {
                    -1
                } else {
                    0
                }
                callOutResult.value = result
            }
            toastShort(it.msg)
        }, {
            callOutResult.value = 0
            toastShort(it.errorMsg)
        }, true)

    }

    fun updateTask(updateTaskRequest: UpdateTaskRequest) {
        request({ apiService.updateTask(updateTaskRequest) }, {
            toastShort("更新成功")
        }, {
            toastShort(it.errorMsg)
        }, false)
    }

    fun updateTask(updateTaskRequest: UpdateTaskMobile,updateSuccess:()->Unit) {
        request({ apiService.updateTask(updateTaskRequest) }, {
            toastShort("更新成功")
            updateSuccess()
        }, {
            toastShort(it.errorMsg)
        }, false)
    }

    /***
     * 根据客户Id 获取客户信息
     */
    fun getCustomInfoById(customId: String) {
        request({ apiService.getCustomInfo(customId) }, {
            customerInfo.value = it
        }, {
            toastShort(it.errorMsg)
        }, true)
    }


}